package com.sjh.baseui.widget

/**
 *
 * @author Extends
 * @date 2019/2/15/015
 */


import android.app.Activity
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.text.TextUtils
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.Constraints
import com.sjh.baseui.R


/**
 * 简单的标题栏控件
 * @author Extends
 * @date 2018/7/18/018
 */

class SimpleTitleView : ConstraintLayout {
    /**
     * title控件
     */
    private var titleView: TextView? = null

    /**
     * 左边的图标控件，一般来说是返回按钮
     */
    private var leftImageView: ImageView? = null

    /**
     * 右边的图标控件
     */
    private var rightImageView: ImageView? = null

    /**
     * 左边的文字控件，默认字体大小会是titleView的0.8倍
     */
    private var leftTextView: TextView? = null

    /**
     * 右边的文字控件，默认字体大小会是titleView的0.8倍
     */
    private var rightTextView: TextView? = null
    private var bottomLine: View? = null

    private var line: View? = null

    /**
     * 控件的默认高度
     */
    private val actionBarSize by lazy { getActionBarSize() }

    /**
     * 默认的图片边长
     */
    private val defImageSide = context.dip(40)

    /**
     * 默认的文字的长度
     */
    private val defTextWidth = context.dip(70)

    /**
     * 默认的图片内边距
     */
    private val defImagePadding = context.dip(7)

    /**
     * 图片内边距
     */
    private var imagePadding = defImagePadding

    /**
     * 右边图标padding
     */
    private var imageRightPadding = imagePadding

    /**
     * 左边图标padding
     */
    private var imageleftPadding = imagePadding


//    /**
//     * 默认的图片内边距
//     */
//    private var ImagePadding = dip(7)
//    /**
//     * 右边图片内边距
//     */
//    private val rightImagePadding = dip(10)

    /**
     * 保存的信息
     */
    private var build: Build =
        Build()

    /**
     * 状态栏的高度
     */
    private var statusBarHeight = 0

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        if (attrs != null) {
            initView(context, attrs, defStyleAttr)
        }
//        initStyle(context, attrs, defStyleAttr)
    }

    private fun initView(context: Context, attrs: AttributeSet, defStyleAttr: Int) {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.SimpleTitleView, defStyleAttr, 0)
        val isFinish = ta.getBoolean(R.styleable.SimpleTitleView_titleLeftIsFinish, false)
        build.setTitle(ta.getString(R.styleable.SimpleTitleView_title)).setTitleBold(
            ta.getBoolean(
                R.styleable.SimpleTitleView_titleBold,
                build.getTitleBold()
            )
        )
            .setTitleSize(
                ta.getDimensionPixelSize(
                    R.styleable.SimpleTitleView_titleSize,
                    build.getTitleSize()
                )
            )
            .setTitleColor(
                ta.getColor(
                    R.styleable.SimpleTitleView_titleColor,
                    build.getTitleColor()
                )
            )
            .setLeftImageRes(
                ta.getResourceId(
                    R.styleable.SimpleTitleView_leftImageRes,
                    build.getLeftImageRes()
                )
            )
            .setRightImageRes(
                ta.getResourceId(
                    R.styleable.SimpleTitleView_rightImageRes,
                    build.getRightImageRes()
                )
            )
            .setLeftText(ta.getString(R.styleable.SimpleTitleView_leftText))
            .setLeftTextSize(
                ta.getDimensionPixelSize(
                    R.styleable.SimpleTitleView_leftTextSize,
                    build.getLeftTextSize()
                )
            )
            .setLeftTextColor(
                ta.getColor(
                    R.styleable.SimpleTitleView_leftTextColor,
                    build.getLeftTextColor()
                )
            )
            .setRightText(ta.getString(R.styleable.SimpleTitleView_rightText))
            .setRightTextSize(
                ta.getDimensionPixelSize(
                    R.styleable.SimpleTitleView_rightTextSize,
                    build.getRightTextSize()
                )
            )
            .setRightTextColor(
                ta.getColor(
                    R.styleable.SimpleTitleView_rightTextColor,
                    build.getRightTextColor()
                )
            )
            .setIsCilpStatusBar(ta.getBoolean(R.styleable.SimpleTitleView_isCilpStatusBar, false))
            .setBottomLineColor(
                ta.getColor(
                    R.styleable.SimpleTitleView_bottom_line_color,
                    build.getBottomLineColor()
                )
            ).setTitleBackground(
                ta.getColor(
                    R.styleable.SimpleTitleView_titleBackground,
                    build.getTitleBackground()
                )
            ).setOnLeftClick {
                if (context is Activity && isFinish) {
                    context.finish()
                }
            }
        imageRightPadding =
            ta.getDimension(R.styleable.SimpleTitleView_rightImagePadding, imagePadding.toFloat())
                .toInt()
        imageleftPadding =
            ta.getDimension(R.styleable.SimpleTitleView_leftImagePadding, imagePadding.toFloat())
                .toInt()
        ta.recycle()
        initFromBuild(build)
    }

    private fun initFromBuild(build: Build) {
        /*********************************   配置各个控件的属性  **********************************/

        if (build.getImagePadding() != 0) {
            imagePadding = build.getImagePadding()
        }
        //设置整体背景颜色
        if (build.getTitleBackground() != 0) {
            setBackgroundColor(build.getTitleBackground())
        }
        // 先判断imagePadding是否修改过，如果修改过则直接使用imagePadding，如果没有修改过再判断
        // Bulid.defImagePadding是否是-1，如果==-1表示没有预设的默认值，则使用defImagePadding
        // 反之则使用Build.defImagePadding
        imagePadding = if (imagePadding == defImagePadding) {
            if (Build.defImagePadding == -1) defImagePadding else Build.defImagePadding
        } else imagePadding

        //配置title属性
        if (build.getTitle() != null) {
            if (titleView == null) {
                titleView = TextView(context)
            }
            titleView!!.setSingleLine()
            titleView!!.ellipsize = TextUtils.TruncateAt.END
            titleView!!.text = build.getTitle()
            titleView!!.paint.isFakeBoldText = build.getTitleBold()
            titleView!!.gravity = Gravity.CENTER
            //设置标题颜色
            if (build.getTitleColor() != 0) {
                titleView!!.setTextColor(build.getTitleColor())
            } else if (Build.defTitleColor != 0) {
                titleView!!.setTextColor(Build.defTitleColor)
            }
            //设置标题大小
            if (build.getTitleSize() != 0) {
                titleView!!.setTextSize(TypedValue.COMPLEX_UNIT_PX, build.getTitleSize().toFloat())
            } else if (Build.defTitleSize != -1f) {
                titleView!!.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                    Build.defTitleSize
                )
            }
        }
        //配置左边imageView
        if (build.getLeftImageRes() != 0 || build.getLeftImageUrl() != null) {
            if (leftImageView == null) {
                leftImageView = ImageView(context)
            }
            leftImageView!!.apply {
                visibility = View.VISIBLE
                setPadding(imagePadding, imagePadding, imagePadding, imagePadding)
                if (build.getRightImageUrl() != null) {
                    Build.defImageLoad?.invoke(leftImageView!!, build.getLeftImageUrl()!!)
                } else {
                    setImageResource(build.getLeftImageRes())
                }
                if (build.getLeftTextColor() != 0) {
                    imageTintList = ColorStateList.valueOf(build.getLeftTextColor())
                }

            }
            if (build.getOnLeftClick() != null) {
                leftImageView!!.setOnClickListener { build.getOnLeftClick()?.invoke(it) }
            }
        } else {
            leftImageView?.visibility = View.GONE
        }
        //配置右边imageView
        if (build.getRightImageRes() != 0 || build.getRightImageUrl() != null) {
            if (rightImageView == null) {
                rightImageView = ImageView(context)
            }
            rightImageView!!.apply {
                visibility = View.VISIBLE
                scaleType = ImageView.ScaleType.CENTER_INSIDE
                setPadding(imagePadding, imagePadding, imagePadding, imagePadding)
                if (build.getRightImageUrl() != null) {
                    Build.defImageLoad?.invoke(rightImageView!!, build.getRightImageUrl()!!)
                } else {
                    setImageResource(build.getRightImageRes())
                }
                if (build.getRightTextColor() != 0) {
                    imageTintList = ColorStateList.valueOf(build.getRightTextColor())
                }
            }
            if (build.getOnRightClick() != null) {
                rightImageView!!.setOnClickListener { build.getOnRightClick()?.invoke(it) }
            }
        } else {
            rightImageView?.visibility = View.GONE
        }
        //配置左边文字
        if (build.getLeftText() != null) {
            if (leftTextView == null) {
                leftTextView = TextView(context)
            }
            leftTextView!!.apply {
                visibility = View.VISIBLE
                text = build.getLeftText()
                setSingleLine()
                setPadding(0, context.dip(5), 0, context.dip(5))
            }
            //配置文字的大小，如果没有单独设置大小，则默认使用titleView的文字大小乘以系数
            if (build.getLeftTextSize() != 0) {
                leftTextView!!.setTextSize(
                    TypedValue.COMPLEX_UNIT_PX,
                    build.getLeftTextSize().toFloat()
                )
            } else if (titleView != null) {
                leftTextView!!.setTextSize(
                    TypedValue.COMPLEX_UNIT_PX,
                    titleView!!.textSize * build.getCoefficient()
                )
            }
            //配置文字的颜色，如果没有单独设置颜色，则默认使用titleView的文字颜色
            if (build.getLeftTextColor() != 0) {
                leftTextView!!.setTextColor(build.getLeftTextColor())
            } else if (titleView != null) {
                leftTextView!!.setTextColor(titleView!!.textColors)
            }
            if (build.getOnLeftClick() != null) {
                leftTextView!!.setOnClickListener {
                    build.getOnLeftClick()?.invoke(leftTextView)
                }
            }
        } else {
            leftTextView?.visibility = View.GONE
        }
        //配置右边文字
        if (build.getRightText() != null) {
            if (rightTextView == null) {
                rightTextView = TextView(context)
            }
            rightTextView!!.apply {
                visibility = View.VISIBLE
                text = build.getRightText()
                gravity = Gravity.RIGHT
                setSingleLine()
                setPadding(0, context.dip(5), 0, context.dip(5))
            }
            //配置文字的大小，如果没有单独设置大小，则默认使用titleView的文字大小乘以系数
            if (build.getRightTextSize() != 0) {
                rightTextView!!.setTextSize(
                    TypedValue.COMPLEX_UNIT_PX,
                    build.getRightTextSize().toFloat()
                )
            } else if (titleView != null) {
                rightTextView!!.setTextSize(
                    TypedValue.COMPLEX_UNIT_PX,
                    titleView!!.textSize * build.getCoefficient()
                )
            }
            //配置文字的颜色，如果没有单独设置颜色，则默认使用titleView的文字颜色
            if (build.getRightTextColor() != 0) {
                rightTextView!!.setTextColor(build.getRightTextColor())
            } else if (titleView != null) {
                rightTextView!!.setTextColor(titleView!!.textColors)
            }
            if (build.getOnRightClick() != null) {
                rightTextView!!.setOnClickListener { build.getOnRightClick()?.invoke(it) }
            }
        } else {
            rightTextView?.visibility = View.GONE
        }
        if (build.getBottomLineColor() != 0) bottomLine = View(context)
        /***********************************   把控件添加到布局中   ********************************/

        //添加基准线
        if (line == null) {
            line = View(context)
            line!!.id = R.id.simple_title_line
        }
        val lineLp = Constraints.LayoutParams(1, 1)
        lineLp.topToTop = ConstraintLayout.LayoutParams.PARENT_ID
        //判断是否需要留出状态栏的高度
        lineLp.topMargin = if (build.isCilpStatusBar()) getStatusBarHeight() else 0
        if (line?.parent == null) {
            addView(line, lineLp)
        } else {
            line!!.layoutParams = lineLp
        }


        if (leftImageView != null && leftImageView?.parent == null) {
            val lp = ConstraintLayout.LayoutParams(defImageSide, defImageSide)
            lp.topToTop = line!!.id
            lp.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
            lp.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID
            lp.leftMargin = imageleftPadding
            addView(leftImageView, lp)
        }
        if (rightImageView != null && rightImageView?.parent == null) {
            val lp = ConstraintLayout.LayoutParams(defImageSide, defImageSide)
            lp.topToTop = line!!.id
            lp.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
            lp.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID
            lp.rightMargin = imageRightPadding
            addView(rightImageView, lp)
        }
        if (leftTextView != null && leftTextView?.parent == null) {
            val lp = ConstraintLayout.LayoutParams(defTextWidth, -2)
            lp.topToTop = line!!.id
            lp.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
            lp.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID
            lp.leftMargin = imageleftPadding
            addView(leftTextView, lp)
        }
        if (rightTextView != null && rightTextView?.parent == null) {
            val lp = ConstraintLayout.LayoutParams(defTextWidth, -2)
            lp.topToTop = line!!.id
            lp.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
            lp.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID
            lp.rightMargin = imageRightPadding
            addView(rightTextView, lp)
        }
        if (titleView != null && titleView?.parent == null) {
            val lp = Constraints.LayoutParams(0, -2)
            lp.topToTop = line!!.id
            lp.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
            lp.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID
            lp.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID
            val horizontalMargin =
                Math.max(defImageSide, defTextWidth + imagePadding) + context.dip(10)
            lp.leftMargin = horizontalMargin
            lp.rightMargin = horizontalMargin
            addView(titleView, lp)
        }
        bottomLine?.let {
            val lp = Constraints.LayoutParams(Constraints.LayoutParams.MATCH_PARENT, context.dip(1))
            it.setBackgroundColor(build.getBottomLineColor())
            lp.bottomToBottom = LayoutParams.PARENT_ID
            lp.startToStart = LayoutParams.PARENT_ID
            addView(it, lp)
        }
    }

    /**
     * 如果高度是wrap_content，则使用默认的高度
     * 默认高度为android.R.attr.actionBarSize
     */
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        //状态栏高度
        val sbh = if (build.isCilpStatusBar()) getStatusBarHeight() else 0

        val heightMode = MeasureSpec.getMode(heightMeasureSpec)

        /**
         * 默认的标题栏的高度，默认为0
         * 如果>0,且测量模式为AT_MOST，则使用defHeight
         * 如果>0,且不满足测量模式为AT_MOST，则使用代码中给height的赋值
         * 如果<=0,且测量模式为AT_MOST，则使用actionBarSize
         * 如果<=0,且不满足测量模式为AT_MOST，则使用代码中给height的赋值
         */
        val heightSize = if (Build.defHeight > 0) {
            if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) Build.defHeight else MeasureSpec.getSize(
                heightMeasureSpec
            )
        } else {
            if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) actionBarSize else MeasureSpec.getSize(
                heightMeasureSpec
            )
        } + sbh
//        println(heightSize)
        val newHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY)
        super.onMeasure(widthMeasureSpec, newHeightMeasureSpec)
    }

    /**获取ActionBar的高度 */
    private fun getActionBarSize(context: Context = getContext()): Int {
        val attrs = intArrayOf(android.R.attr.actionBarSize)
        val values = context.theme.obtainStyledAttributes(attrs)
        try {
            return values.getDimensionPixelSize(0, context.dip(48))//第一个参数数组索引，第二个参数 默认值
        } finally {
            values.recycle()
        }
    }

    /**
     * 设置标题
     */
    fun setTitleText(title: String) {
        build.setTitle(title)
        if (titleView != null) {
            titleView!!.text = title
        }
    }

    /**
     * 设置标题alpha
     */
    fun setTitleAlpha(alpha: Float) {
        titleView?.alpha = alpha
    }

    /**
     * 设置左边的文本或者图片的点击事件
     */
    fun setOnLeftClick(click: (v: View?) -> Unit) {
        build.setOnLeftClick(click)
        if (leftImageView != null) {
            leftImageView!!.setOnClickListener { click(it) }
        } else if (leftTextView != null) {
            leftTextView!!.setOnClickListener { click(it) }
        }
    }

    fun setLeftImageRes(imageRes: Int) {
        build.setLeftImageRes(imageRes)
        if (leftImageView == null) {
            initFromBuild(build)
        } else {
            leftImageView?.setImageResource(imageRes)
        }
    }

    /**
     * 设置右边的文本或者图片的点击事件
     */
    fun setOnRightClick(click: (v: View?) -> Unit) {
        build.setOnRightClick(click)
        if (rightImageView != null) {
            rightImageView!!.setOnClickListener { click(it) }
        } else if (rightTextView != null) {
            rightTextView!!.setOnClickListener { click(it) }
        }
    }

    fun setRightImageRes(imageRes: Int) {
        build.setRightImageRes(imageRes)
        if (rightImageView == null) {
            initFromBuild(build)
        } else {
            rightImageView?.setImageResource(imageRes)
        }
    }

    /**
     * 改变配置数据
     */
    fun change(change: Build.() -> Unit) {
        build.change()
        initFromBuild(build)
    }

    fun change(titleBean: TitleBean) {
        change {
            //标题栏
            if (titleBean.backgroundColor != null) {
                setBackgroundColor(Color.parseColor(titleBean.backgroundColor))
            }
            if (titleBean.titleColor != null) {
                setTitleColor(Color.parseColor(titleBean.titleColor))
            }
            if (titleBean.title != null) {
                setTitle(titleBean.title)
            }

            //左边布局
            if (titleBean.leftImage != null) {
                setLeftImageRes(0)
                setLeftImageUrl(titleBean.leftImage)
                setLeftText(null)
            } else if (titleBean.leftText != null) {
                setLeftImageRes(0)
                setLeftImageUrl(null)
                setLeftText(titleBean.leftText)
            }

            if (titleBean.leftColor != null) {
                setLeftTextColor(Color.parseColor(titleBean.leftColor))
            }

            //右边布局
            if (titleBean.rightImage != null) {
                setRightImageRes(0)
                setRightImageUrl(titleBean.rightImage)
                setRightText(null)
            } else if (titleBean.rightText != null) {
                setRightImageRes(0)
                setRightImageUrl(null)
                setRightText(titleBean.rightText)
            }

            if (titleBean.rightColor != null) {
                setRightTextColor(Color.parseColor(titleBean.rightColor))
            }

        }
    }


    /**
     * 获取Build
     */
    fun getBuild(): Build {
        return build
    }

    private fun getStatusBarHeight(): Int {
        // 获得状态栏高度
        if (statusBarHeight == 0) {
            val resourceId =
                context.resources.getIdentifier("status_bar_height", "dimen", "android")
            statusBarHeight = context.resources.getDimensionPixelSize(resourceId)
        }
        return statusBarHeight
    }


    /**
     * 返回右边ImageView
     */
    fun getRightImage(): ImageView? {
        return rightImageView
    }

    /**
     * 返回右边ImageView
     */
    fun getRightTextView(): TextView? {
        return rightTextView
    }

    class Build {
        /**
         * title文字
         */
        private var title: String? = null

        /**
         * title字体大小，单位是px
         */
        private var titleSize: Int = 0

        /**
         * title字体颜色
         */
        private var titleColor: Int = 0

        /**
         * title是否是粗体
         */
        private var isBold: Boolean = true

        /**
         * 左边的图片资源，一般是返回按钮
         */
        private var leftImageRes: Int = 0

        /**
         * 右边的图片资源
         */
        private var rightImageRes: Int = 0

        /**
         * 左边的图片资源,优先级比本地资源高
         */
        private var leftImageUrl: String? = null

        /**
         * 右边的图片资源,优先级比本地资源高
         */
        private var rightImageUrl: String? = null

        /**
         * 左边的文字
         */
        private var leftText: String? = null

        /**
         * 左边文字大小，单位是px
         */
        private var leftTextSize: Int = 0

        /**
         * 左边文字颜色
         */
        private var leftTextColor: Int = 0

        /**
         * 右边的文字
         */
        private var rightText: String? = null

        /**
         * 右边文字大小，单位是px
         */
        private var rightTextSize: Int = 0

        /**
         * 右边文字颜色
         */
        private var rightTextColor: Int = 0

        /**
         * 底部分割线颜色
         */
        private var bottomLineColor: Int = 0

        /**
         * 系数，当没有给leftText和rightText设置文字大小时
         * 默认给它们设置title字体大小的系数倍
         */
        private var coefficient = 0.8f

        /**
         * 右边按钮的点击事件
         */
        private var onRightClick: ((v: View?) -> Unit)? = null

        /**
         * 左边按钮的点击事件
         */
        private var onLeftClick: ((v: View?) -> Unit)? = null

        /**
         * 是否在顶部留出状态栏的高度
         */
        private var isCilpStatusBar = false

        /**
         * 图标的内边距，单位是px
         */
        private var imagePadding = 0

        /**
         *整体背景颜色
         **/
        private var titleBackground = 0

        fun getImagePadding() = imagePadding

        fun setImagePadding(imagepadding: Int) {
            this.imagePadding = imagepadding
        }


        fun getTitle(): String? {
            return title
        }

        fun setTitle(title: String?): Build {
            this.title = title
            return this
        }

        fun getTitleSize(): Int {
            return titleSize
        }

        fun setTitleBold(isBold: Boolean): Build {
            this.isBold = isBold
            return this
        }

        fun getTitleBold(): Boolean {
            return this.isBold
        }

        fun setTitleSize(titleSize: Int): Build {
            this.titleSize = titleSize
            return this
        }

        fun getTitleColor(): Int {
            return titleColor
        }

        fun setTitleColor(titleColor: Int): Build {
            this.titleColor = titleColor
            return this
        }

        fun getTitleBackground(): Int {
            return titleBackground
        }

        fun setTitleBackground(backgroundColor: Int): Build {
            this.titleBackground = backgroundColor
            return this
        }

        fun getLeftImageRes(): Int {
            return leftImageRes
        }

        fun setLeftImageRes(leftImageRes: Int): Build {
            this.leftImageRes = leftImageRes
            return this
        }

        fun getRightImageRes(): Int {
            return rightImageRes
        }

        fun setRightImageRes(rightImageRes: Int): Build {
            this.rightImageRes = rightImageRes
            return this
        }

        fun getLeftImageUrl(): String? {
            return leftImageUrl
        }

        fun setLeftImageUrl(leftImageUrl: String?): Build {
            this.leftImageUrl = leftImageUrl
            return this
        }

        fun getRightImageUrl(): String? {
            return rightImageUrl
        }

        fun setRightImageUrl(rightImageUrl: String?): Build {
            this.rightImageUrl = rightImageUrl
            return this
        }

        fun getLeftText(): String? {
            return leftText
        }

        fun setLeftText(leftText: String?): Build {
            this.leftText = leftText
            return this
        }

        fun getLeftTextSize(): Int {
            return leftTextSize
        }

        fun setLeftTextSize(leftTextSize: Int): Build {
            this.leftTextSize = leftTextSize
            return this
        }

        fun getLeftTextColor(): Int {
            return leftTextColor
        }

        fun setLeftTextColor(leftTextColor: Int): Build {
            this.leftTextColor = leftTextColor
            return this
        }

        fun getRightText(): String? {
            return rightText
        }

        fun setRightText(rightText: String?): Build {
            this.rightText = rightText
            return this
        }

        fun getRightTextSize(): Int {
            return rightTextSize
        }

        fun setRightTextSize(rightTextSize: Int): Build {
            this.rightTextSize = rightTextSize
            return this
        }

        fun getRightTextColor(): Int {
            return rightTextColor
        }

        fun setRightTextColor(rightTextColor: Int): Build {
            this.rightTextColor = rightTextColor
            return this
        }

        fun getBottomLineColor(): Int {
            return bottomLineColor
        }

        fun setBottomLineColor(bottomLineColor: Int): Build {
            this.bottomLineColor = bottomLineColor
            return this
        }

        fun getCoefficient(): Float {
            return coefficient
        }

        fun setCoefficient(coefficient: Float): Build {
            this.coefficient = coefficient
            return this
        }

        fun setOnRightClick(click: (v: View?) -> Unit): Build {
            this.onRightClick = click
            return this
        }

        fun setOnLeftClick(click: (v: View?) -> Unit): Build {
            this.onLeftClick = click
            return this
        }

        fun getOnLeftClick() = onLeftClick

        fun getOnRightClick() = onRightClick

        fun setIsCilpStatusBar(isCilpStatusBar: Boolean): Build {
            this.isCilpStatusBar = isCilpStatusBar
            return this
        }

        fun isCilpStatusBar() = this.isCilpStatusBar


        companion object {
            /**
             * 默认的标题栏的高度，默认为0
             * 如果>0,且测量模式为AT_MOST，则使用defHeight
             * 如果>0,且不满足测量模式为AT_MOST，则使用代码中给height的赋值
             * 如果<=0,且测量模式为AT_MOST，则使用actionBarSize
             * 如果<=0,且不满足测量模式为AT_MOST，则使用代码中给height的赋值
             */
            @Deprecated("建议使用style")
            var defHeight = 0

            /**
             * 默认的图片内边距，默认-1，表示使用控件内部的默认值
             * 如果>-1,且控件内的内边距值也是默认值，表示使用当前的默认值，而不是使用控件内部的默认值
             * 如果控件内不是默认值，则还是优先使用控件内的值
             */
            @Deprecated("建议使用style")
            var defImagePadding = -1

            var defImageLoad: ((imageView: ImageView, url: String) -> Unit)? = null

            /**
             * 默认的标题文字大小，单位px
             */
            @Deprecated("建议使用style")
            var defTitleSize = -1f

            /**
             * 默认的标题文字颜色
             */
            @Deprecated("建议使用style")
            var defTitleColor = Color.WHITE
        }
    }

    /**
    "leftImage":"http://yd51.51liuliuqiu.cn/static/login/images/close.png",
    "leftColor":"#ffffff",
    "leftText","左边文字",
    "leftOnClick":"",
    "title":"测试标题",
    "titleColor":"#ffffff",
    "rightImage":"http://yd51.51liuliuqiu.cn/static/login/images/close.png",
    "rightColor":"#ffffff",
    "rightText":"右边文字",
    "rightOnClick":"",
    "backgroundColor":"#ffffff",
    "visibility":true
     */

    /**
     *  leftImage的优先级比leftText大，
     *  设置了leftColor后，对于图片，则是设置图片的Tint；对于文字，则是设置文字fontColor
     */
    data class TitleBean(
        val leftImage: String? = null,
        val leftColor: String? = null,
        val leftText: String? = null,
        val leftOnClick: String? = null,
        val title: String? = null,
        val titleColor: String? = null,
        val rightImage: String? = null,
        val rightColor: String? = null,
        val rightText: String? = null,
        val rightOnClick: String? = null,
        val backgroundColor: String? = null,
        val visibility: Boolean? = null
    )

    fun Context.dip(value: Int): Int = (value * resources.displayMetrics.density).toInt()
}